package com.itany.shop.servlet;

import com.itany.shop.entity.User;
import com.itany.shop.exception.*;
import com.itany.shop.factory.ObjectFactory;
import com.itany.shop.service.UserService;
import com.itany.shop.vo.Cart;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Author:shixiaojun@itany.com
 * Date:2022/4/14 14:23
 * Version:1.0-SNAPSHOT
 * Description:
 */
@WebServlet("*.user")
public class UserServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String path = request.getServletPath();
        if("/register.user".equals(path)){
            register(request,response);
        }
        if("/login.user".equals(path)){
            login(request,response);
        }
        if("/logout.user".equals(path)){
            logout(request,response);
        }
    }

    public void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getSession().invalidate();
        response.sendRedirect(request.getContextPath()+"/findAll.product");
    }

    public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserService userService = (UserService) ObjectFactory.getObject("userService");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String code = request.getParameter("code");
        String image = (String) request.getSession().getAttribute("code");

        try {
            User user = userService.login(username,password,code,image);
            // 在执行登录操作的时候,基本上所有的项目都会将登录成功的用户信息保存在session
            // 便于登录之后使用当前登录的员工的信息
            request.getSession().setAttribute("user",user);

            // 当用户登录成功进入到商城之后
            // 可以获取一个新的购物车,并将该购物车存放到session
            // 便于购物车数据的CURD操作
            request.getSession().setAttribute("cart",new Cart());

            // 登录成功之后,跳转到主页,此时主页存在后台数据
            // 因此,跳转到对应的Servlet
            response.sendRedirect(request.getContextPath()+"/findAll.product");
        } catch (RequestParameterErrorException e) {
            request.setAttribute("loginMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        } catch (CodeErrorException e) {
            request.setAttribute("loginMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        } catch (UserNotExistException e) {
            request.setAttribute("loginMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        } catch (ServiceException e) {
            request.setAttribute("loginMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        }  catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("loginMsg","服务器内部异常");
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        }


    }

    public void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserService userService = (UserService) ObjectFactory.getObject("userService");
        // 获取请求参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String phone = request.getParameter("phone");
        String address = request.getParameter("address");

        try {
            userService.register(username,password,phone,address);
            response.sendRedirect(request.getContextPath()+"/jsp/login.jsp");
        } catch (RequestParameterErrorException e) {
            request.setAttribute("registerMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/register.jsp").forward(request,response);
        } catch (UserExistException e) {
            request.setAttribute("registerMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/register.jsp").forward(request,response);
        } catch (ServiceException e) {
            request.setAttribute("registerMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/register.jsp").forward(request,response);
        }  catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("registerMsg","服务器内部异常");
            request.getRequestDispatcher("/jsp/register.jsp").forward(request,response);
        }

    }

}
